<template>
  <div class="app-container">
    <div
      style="
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        margin-bottom: 20px;
      "
    >
      <div>
        <span
          style="
            font-size: 14px;
            color: #606266;
            font-weight: 700;
            margin-right: 12px;
          "
        >学生姓名</span
        >
        <el-input
          size="small"
          class="search_item"
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </div>
      <div>
        <span
          style="
            font-size: 14px;
            color: #606266;
            font-weight: 700;
            margin-right: 12px;
          "
        >课程</span
        >
        <el-select
          filterable
          clearable
          size="small"
          class="search_item"
          v-model="queryParams.courseId"
          placeholder="请选择课程"
        >
          <el-option
            v-for="item in courseList"
            :key="item.courseId"
            :label="item.courseName"
            :value="item.courseId"
          >
          </el-option>
        </el-select>
      </div>
      <el-button
        v-preventReClick
        type="primary"
        icon="el-icon-search"
        size="mini"
        @click="handleQuery"
      >搜索
      </el-button
      >
      <el-button
        v-preventReClick
        icon="el-icon-refresh"
        size="mini"
        @click="resetQuery"
      >重置
      </el-button
      >
    </div>

    <el-table
      v-loading="loading"
      :data="studentList"
      height="calc(100vh - 250px)"
    >
      <el-table-column label="学号" align="center" prop="studentNum"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column
        :label="`${$gradeName ? $gradeName : '班级'}`"
        align="center"
        prop="className"
      />
      <el-table-column
        :label="`${$courseName ? $courseName : '课程'}——${
          $workstationName ? $workstationName : '工作站'
        }`"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          {{ scope.row.courseName }}——{{
            scope.row.workstationName ? scope.row.workstationName : '暂无'
          }}
        </template>
      </el-table-column>
      <el-table-column label="主讲教师" align="center" prop="principalName"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handlePassTheRemainingLevels(scope.row)"
          >通过剩余关
          </el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #e95756"
            @click="handleResetKickOut(scope.row)"
          >重置踢出
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel"
    >
      <div style="display: flex">
        <div
          style="
            width: 40px;
            font-size: 14px;
            color: #606266;
            font-weight: 700;
            margin-right: 12px;
          "
        >
          原因:
        </div>
        <el-input
          maxlength="500"
          show-word-limit
          :rows="5"
          :autosize="{ minRows: 5, maxRows: 8 }"
          v-model="reason"
          type="textarea"
          placeholder="请输入原因"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
        >确 定
        </el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      style="margin-top: 15vh"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="剩余未考核内容打分"
      :visible.sync="showFinishDialog"
      width="500px"
      append-to-body
      @close="Finishcancel()"
    >
      <el-form
        ref="form"
        @submit.native.prevent
        :model="form"
        :rules="rules"
        label-width="90px"
      >
        <div class="sign" style="color: red">
          <span>注:</span> 剩余所有未考核内容，以{{
            form.score ? form.score : '--'
          }}分（百分制）卷面分给分
        </div>
        <el-form-item label="学号" prop="studentNum" style="margin-top: 20px">
          {{ form.studentNum }}
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          {{ form.name }}
        </el-form-item>
        <el-form-item label="班级" prop="className">
          {{ form.className }}
        </el-form-item>
        <el-form-item label="课程-工作站" prop="courseName">
          {{ form.courseName }}——{{
            form.workstationName ? form.workstationName : '暂无'
          }}
        </el-form-item>
        <el-form-item label="分数" prop="score" placeholder="请输入分数">
          <el-input
            oninput="value=value.replace(/[^0-9]|^0+(?!$)/g,''); if(parseInt(value)>100) value='100';"
            v-model="form.score"
            placeholder="请输入分数"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Finishcancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { allCourseList } from '@/api/course/course'
import { finishResidualIndex, kickOutStudent, courseStuList } from '@/api/home'

export default {
  name: 'Post',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      showFinishDialog: false,
      curItem: {},
      form: {
        score: ''
      },
      rules: {
        score: [
          {
            required: true,
            message: '请输入分数',
            trigger: 'blur'
          }
        ]
      },
      // 遮罩层
      loading: false,
      //重置踢出&&通过剩余关
      open: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      // 岗位表格数据
      studentList: [],
      // 课程列表数据
      courseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        courseId: ''
      },
      reason: '',
      title: '',
      studentId: '',
      workstationId: '',
      courseId: '',
      courseSchedulingId: ''
    }
  },
  activated() {
  },
  created() {
    this.getList()
    this.getCourseList()
  },
  methods: {
    Finishcancel() {
      this.resetForm('form')
      this.showFinishDialog = false
    },
    submitForm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.$confirm(`是否确认打分？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async() => {
              const loading = this.$loading({
                lock: true,
                text: '处理中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.3)'
              })
              let res
              try {
                res = await finishResidualIndex(
                  this.curItem.courseStuId,
                  this.form.score
                )
              } catch (e) {
                loading.close()
              }
              const { code } = res
              if (code == 200) {
                loading.close()
                this.$modal.msgSuccess('处理成功')
                this.showFinishDialog = false
                this.getList()
              } else {
                loading.close()
              }
            })
            .catch(() => {
              this.$modal.msgInfo('已取消')
            })
        }
      })
    },

    cancel() {
      this.reason = ''
      this.open = false
    },
    getCourseList() {
      let params = {
        pageNum: 1,
        pageSize: 9999,
        status: 0,
        temporaryState: 1
      }
      allCourseList(params).then((response) => {
        this.courseList = response.rows
      })
    },
    /** 查询学生列表 */
    getList() {
      this.loading = true
      // 如果url带班级id
      courseStuList(this.queryParams)
        .then((response) => {
          this.studentList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    handlePassTheRemainingLevels(row) {
      this.curItem = row
      this.form = row
      this.showFinishDialog = true
    },
    handleResetKickOut(row) {
      if (row.isLeader == 1) {
        Message({ message: `该学生为小组组长，无法踢出`, type: 'warning' })
        return
      }

      this.$confirm(
        '该学生将被移出此课程，学习进度及考核记录清空，确认重置并踢出吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async() => {
          let res = await kickOutStudent(row.courseStuId)
          if (res.code == 200) {
            this.$modal.msgSuccess('踢出成功')
            this.getList()
          }
        })
        .catch(() => {
          this.$modal.msgInfo('已取消')
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: '',
        courseId: ''
      }
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.search_item {
  width: 215px;
  margin-right: 12px;
}
</style>
